package com.aaa.dang.modules.mobilereview.service.Impl;

import com.aaa.dang.common.entity.User;
import com.aaa.dang.common.util.BaseContrllor;
import com.aaa.dang.common.util.ResultModel;
import com.aaa.dang.modules.mobilereview.dao.IMobileReviewDao;
import com.aaa.dang.modules.mobilereview.service.IMobileReviewService;
import com.aaa.dang.modules.user.dao.IUserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Map;

import static com.aaa.dang.common.systemconst.ISystemConst.SUCCESS_CODE;

/**
 * @Author: ymk
 * @Date: 2018/12/27 11:15
 * @description:
 */
@Service
public class MobileReviewServiceImpl extends BaseContrllor implements IMobileReviewService {

    @Autowired
    private IMobileReviewDao dao;

    @Autowired
    private ResultModel model;

    @Autowired
    private IUserDao userDao;

    /**
     * 发送入住申请
     * @param query
     * @return
     */

    @Override
    @Transactional(rollbackFor=Exception.class)
    public ResultModel liveApply(Map<String,Object> query) {
        int i = dao.addliveCheck(query);
        if(i>SUCCESS_CODE){
            model=returnSuccessInfo("申请发送成功");
        }else{
            model=returnErrorInfo("申请发送失败");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }


        return model;
    }

    /**
     * 退宿申请
     * @param query
     * @return
     */

    @Override
    @Transactional(rollbackFor=Exception.class)
    public ResultModel retireApply(Map<String, Object> query) {
        try{
            int bed_id = dao.getbed_id(query);
            query.put("bed_id",bed_id);
            int i = dao.addretireCheck(query);
            model=returnSuccessInfo("申请发送成功");

        }catch(Exception e){
            model=returnErrorInfo("申请发送失败");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return model;
    }

    /**
     * 添加押金审核
     * @param query
     * @return
     */

    @Override
    @Transactional(rollbackFor=Exception.class)
    public ResultModel addpositCheck(Map<String, Object> query) {
        try{
            int i = dao.addpositCheck(query);
            String username= (String) query.get("username");
            User user = userDao.getUser(username);
            model=returnSuccessInfo("申请发送成功");
            model.setData(user);
        }catch (Exception e){
            model=returnErrorInfo("申请发送失败");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }

        return model;
    }

    /**
     * 添加租金审核
     * @param query
     * @return
     */

    @Override
    @Transactional(rollbackFor=Exception.class)
    public ResultModel addrentCheck(Map<String, Object> query) {
        try{
            int i = dao.addrentCheck(query);
            model=returnSuccessInfo("申请发送成功");
        }catch (Exception e){
            model=returnErrorInfo("申请发送失败");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }

        return model;
    }
}
